package com.polaris.live.ui.live.activity

import android.os.Bundle
import android.view.View.GONE
import com.gyf.immersionbar.ImmersionBar
import com.polaris.live.adapter.match.MatchingAdapter
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.ActivityStartMatchBinding
import com.polaris.live.resp.event.CallEndEvent
import com.polaris.live.utils.ScaleTransformer
import com.polaris.live.utils.StartCallProxy
import com.polaris.live.utils.getIntOrString
import com.polaris.live.utils.getLongOrString
import com.polaris.live.viewmodel.home.MatchViewModel
import com.zhpan.bannerview.constants.PageStyle
import com.zhpan.indicator.enums.IndicatorSlideMode
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.properties.Delegates

/**
 * StartMatchActivity
 *
 * @author Created by 半仙 on 2023/10/24/024 11:30
 */
class StartMatchActivity : BaseVbActivity<MatchViewModel, ActivityStartMatchBinding>() {

    private val mAdapter by lazy { MatchingAdapter() }

    private var matchId by Delegates.notNull<Long>()

    private val mPage by lazy { intent.getIntOrString("page", UserConst.From.UNKNOWN) }
    private val mLastPage by lazy { intent.getIntOrString("lastPage", UserConst.LastFrom.UNKNOWN) }

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .statusBarDarkFont(false)
            .titleBar(mBinding.closeCall)
            .init()
    }

    override fun initView(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)

        matchId = intent.getLongOrString("id", 0L)

        mBinding.bannerView.apply {
            registerLifecycleObserver(lifecycle)
            setIndicatorSlideMode(IndicatorSlideMode.NORMAL)
            setIndicatorVisibility(GONE)
        }

        initListener()
        initObserver()

        mViewModel.matchAnimation()

        mViewModel.launchCallHeartbeat(matchId)
    }

    private fun initObserver() {
        mViewModel.matchNextLiveData.observe(this) {
            mViewModel.launchCallHeartbeat(it)
        }
        mViewModel.matchLiveData.observe(this) { resultState ->
            parseState(resultState, {
                if (it.user != null) {
                    mViewModel.endMatch(it.matchId, CommonConst.MatchEnd.END_MATCH)

                    val userId = it.user.base.userId
                    StartCallProxy.builder(this).startCall(
                        mViewModel,
                        mPage,
                        mLastPage,
                        LiveConst.SenderFrom.MATCH,
                        userId,
                        it.user.base,
                        it.matchId,
                        {
                            finish()
                        }
                    )
                }
            }, {
                if (it.errCode == CommonConst.ErrorCode.MATCH_END) {
                    mViewModel.rematch()
                }
            })
        }
        mViewModel.rematchLiveData.observe(this) { state ->
            parseState(state, {
                matchId = it.id
            }, {
                ToastGravity.showGravity(it.errorMsg)

                finish()
            })
        }
        mViewModel.matchAnimaLiveData.observe(this) { resultState ->
            parseState(resultState, {
                mBinding.bannerView.setAdapter(mAdapter)
                    .setPageMargin(28)
                    .setScrollDuration(500)
                    .setRevealWidth(140.dp, 140.dp)
                    .setPageStyle(PageStyle.MULTI_PAGE_SCALE, 0f)
                    .setPageTransformer(ScaleTransformer())
                    .create(it)
            })
        }
    }

    private fun initListener() {
        mBinding.closeCall.setOnSingleClickListener {
            mViewModel.endMatch(matchId, CommonConst.MatchEnd.MANUAL)
            finish()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: CallEndEvent) {
        mViewModel.endMatch(matchId, CommonConst.MatchEnd.MANUAL)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}